const { Controller } = require('egg')

class LikeController extends Controller {
    async like() {
        const { ctx } = this;
        const { videoid } = ctx.params
        const { _id } = ctx.userinfo
        try {
            const dbback = await ctx.service.like.getlike(videoid, _id)
            if (!dbback) {
                await ctx.service.like.setlike({ videoid, userid: _id }, 1)
                const {likecount} = await ctx.service.video.videodetail(videoid)
                await ctx.service.video.updatevideo(videoid,{likecount:likecount+1})
                await ctx.service.redis.createHotc(videoid,3)
                ctx.body = {
                    code: 0,
                    msg: '设置为喜欢'
                }
            }else{
                const {like,_id} = dbback
                if(like === 1){
                    ctx.body = {
                        code: 0,
                        msg: '已经是喜欢的视频了'
                    }
                }else if(like === 0){
                    await ctx.service.like.updatelike(_id, 1)
                    const {likecount,dislikecount} = await ctx.service.video.videodetail(videoid)
                    if(dislikecount > 0){
                        await ctx.service.video.updatevideo(videoid,{dislikecount:dislikecount-1})
                    }
                    await ctx.service.video.updatevideo(videoid,{likecount:likecount + 1})
                    await ctx.service.redis.createHotc(videoid,3)
                    ctx.body = {
                        code: 0,
                        msg: '设置喜欢成功'
                    }
                }
            }
        } catch {
            ctx.body = {
                code: -1,
                msg: '设置喜欢失败'
            }
        }
    }

    async list(){
        const { ctx } = this;
        const { page, pagesize } = ctx.query
        const { _id } = ctx.userinfo
        try {
            const { likes, count } = await ctx.service.like.list(_id,page, pagesize)
            ctx.body = {
                code: 0,
                msg: '获取喜欢列表成功',
                data: likes,
                count
            }
        } catch {
            ctx.body = {
                code: -1,
                msg: '获取喜欢列表失败'
            }
        }
    }


    async dislike() {
        const { ctx } = this;
        const { videoid } = ctx.params
        const { _id } = ctx.userinfo
        try {
            const dbback = await ctx.service.like.getlike(videoid, _id)
            if (!dbback) {
                await ctx.service.like.setlike({ videoid, userid: _id }, 0)
                const {dislikecount} = await ctx.service.video.videodetail(videoid)
                await ctx.service.video.updatevideo(videoid,{dislikecount:dislikecount + 1})
                await ctx.service.redis.createHotc(videoid,-1)
                ctx.body = {
                    code: 0,
                    msg: '设置为不喜欢'
                }
            }else{
                const {like,_id} = dbback
                if(like === 0){
                    ctx.body = {
                        code: 0,
                        msg: '已经是不喜欢的视频了'
                    }
                }else if(like === 1){
                    await ctx.service.like.updatelike(_id, 0)
                    const {likecount,dislikecount} = await ctx.service.video.videodetail(videoid)
                    if(likecount > 0){
                        await ctx.service.video.updatevideo(videoid,{likecount:likecount-1})
                    }
                    await ctx.service.video.updatevideo(videoid,{dislikecount:dislikecount + 1})
                    await ctx.service.redis.createHotc(videoid,-1)
                    ctx.body = {
                        code: 0,
                        msg: '设置为不喜欢'
                    }
                }
            }
        } catch {
            ctx.body = {
                code: -1,
                msg: '设置喜欢失败'
            }
        }
    }


}

module.exports = LikeController;